import { Request, Response, NextFunction } from 'express';
import _ from 'lodash';
import { getPickUp, createPickUp, updatePickUp, deletePickUp, getIdPickUp, statusChange, byKeyWordSelect } from './pickUp.service';
import { getFavorite } from '../favorites/favorites.service';
import { cornerDestroyById } from '../corner/common.service';
import { createNotice } from '../notification/notification.service';

/**
 * 获取内容
 */
export const index = async (
  request: Request,
  response: Response,
  next: NextFunction
) => {
  let status = request.query.status
  let pageNo = request.query.pageNo

  let user = request.user;
  let currentUser = request.query.currentUser;
  const uid = request.query.userId;
  let userId = uid ? parseInt(`${uid}`, 10) : 0;
  if (user && user.id > 0 && currentUser) {
    userId = user.id;
  } else if (!user && currentUser) {
    response.send({ data: {} })
    return;
  }

  const data = await getPickUp(parseInt(`${status}`, 10), 10, parseInt(`${pageNo}`, 10), userId);
  
  response.send(data);
}

/**
 * 获取内容(按 ID 查找)
 */
export const pickUpDetail = async (
  request: Request,
  response: Response,
  next: NextFunction
) => {
  // 获取内容 ID
  const { postId } = request.params;

  // 调取内容
  try {
    const data = await getIdPickUp(parseInt(postId, 10));

    const user = request.user;
    if (user && user.id > 0) {
      let favoriteParam = { user_id: user.id, type: 7, target_id: data.id }
      let favorite = await getFavorite(favoriteParam);
      data.favorite = (favorite as any).length > 0 ? favorite[0] : null;
    }

    // 做出响应
    response.send(data);
  } catch (error) {
    next(error);
  }
};

/**
 * 模糊查询
 */
export const selectByKey = async (
  request: Request,
  response: Response,
  next: NextFunction
) => {
  // 获取关键字
  const { keyWord } = request.query;

  try {
    const data = await byKeyWordSelect(keyWord)
    response.send(data)
  } catch (error) {
    next(error)
  }
}

/**
 * 创建内容
 */
export const store = async (
  request: Request,
  response: Response,
  next: NextFunction
) => {
  // 获取内容
  let pickUpModel  = request.body;

  pickUpModel.status = 0;
  let user = request.user;
  if (user.type != 1) {
    pickUpModel.user_id = user.id
  }

  // 创建内容
  if (pickUpModel.id && pickUpModel.id > 0) {

    const data = await updatePickUp(pickUpModel, parseInt(pickUpModel.id, 10));
    response.send(data);
  } else {
    const data = await createPickUp(pickUpModel);
    response.send(data);
  }
}

/**
 * 更新内容
 */
export const update = async (
  request: Request,
  response: Response,
  next: NextFunction
) => {
  // 获取更新内容 ID
  const { postId } = request.params;

  // 获取更新内容
  const post = _.pick(request.body, ['status', 'remark_zh', 'remark_ug', 'name_zh', 'name_ug', 'where_zh', 'where_ug', 'phoneNum', 'detailed_zh', 'detailed_ug']);

  // 更新内容
  try {
    const data = await updatePickUp(post, parseInt(postId, 10));
    response.send(data);
  } catch (error) {
    next(error);
  }
}

/**
 * 删除内容
 */
export const destroy = async (
  request: Request,
  response: Response,
  next: NextFunction
) => {
  // 获取删除内容 ID
  let { postId } = request.body;
  if (postId == undefined) postId = request.params.postId

  // 删除内容
  try {
    const data = await deletePickUp(parseInt(postId, 10));
    if (data) await cornerDestroyById(parseInt(postId, 10), 7);
    response.send(data);
  } catch (error) {
    next(error);
  }
}

/**
 * 状态
 */
export const status = async (
  request: Request,
  response: Response,
  next: NextFunction,
) => {
  // 获取内容 ID
  const postId = request.body.id;

  // 获取内容
  const post = _.pick(request.body, ['status', 'remark_zh', 'remark_ug']);

  // 执行
  try {
    let pickUp = await getIdPickUp(postId);
    const data = await statusChange(parseInt(postId, 10), post);
    if (data) {
      let notice = {
        type: 2,
        target_id: postId,
        target_type: 7,
        content_ug: post.remark_ug,
        content_zh: post.remark_zh,
        status: 0
      }
      // await createNotification(notice, pickUp.user_id)
    }
    response.send(data);
  } catch (error) {
    next(error);
  }
};
